[Note: I have tried to post normally this followup, but it seam it
didn't work; this follows news:4hdctd$r1p@engnews1.Eng.Sun.COM (Steve
Clamage's article) Please remove this note before posting. ]
clamage@Eng.Sun.COM (Steve Clamage) wrote:
>Valentin Bonnard <bonnardv@pratique.fr> writes:
>
>>I have 4 ideas about C++ and a question.
>
>>1) String literals
>>~~~~~~~~~~~~~~~~~~
>>String literals should be const char* instead of char* (if it is not
>>already the case).
>
>Yes, it is clearly the "right" thing to do, but it is not the case
>for the same reason it is not the case in Standard C: it would break
>too much existing code to make the change.
Perhaps, but in C void* converted to anything* and it's not the case in C++: this broke more C code (I think, or a least more of my old C code) then what I propose.
Or could it be a compiler option (that all compilers should have) to make string literals char* ?
>>2) Placement operators
>>~~~~~~~~~~~~~~~~~~~~~~
>>class Object {
>> void operator+ // or / or whatever
>> (Object& result, const Object& b) const;
binary (compute *this + b, put the result in 'result')
>> void operator* (Object& result) const; #2
unary (compute *(*this), put the result in 'result')
>> Object operator* (const Object& result) const; // old one
^^^^^^ ops! should be named b
binary (compute *this + b, return the result)
>>}
>
>>should be called respectively by:
>>a = b + c;
>
>>and a = *b;
>
>>and (old one) a*b;
>
>>This will solve the [well-known] problem of copying the result into a temporary.
>>The compatibility will be preserved since old syntax will be allowed to.
>
>You will have to present this idea in more detail. I can't figure out
>what you are proposing. It looks like you want the function marked #2
>to be some form of unary dereferencing operator, but that would change
>its existing meaning of a member binary (multiplication) operator.
The story begin with a class Object for the construction and destruction is not trivial (and take some time):
Object a, b, c;
a = b @ c;
where @ is one of: + - * / % & | ^ && || etc... (but not one of: += -= *= /= %= &= |= ^=)
I want to be able to write (using the friend notation to make it a little more clear):
Object operator @ (const Object& a, const Object& b);
The definition would become (for example):
void operator @ (Object& result, const Object& a, const Object& b)
{
result = a;
result @= b; // direct operation
}
rather than
Object operator @ (const Object& a, const Object& b)
{
Object result = a;
result @= b;
return Object; // temporary created with copy constructor, Object deleted,
} // then temporary copied into a, and temporary deleted
The old style should still be allowed, and the operator signature is clearly distinct from the one I propose.
For unary operators, the idea is exactly the same, but it's a bit more difficult to see if it's a binary operator or a unary operator because my syntax and an argument:
(where @ is one of ! ~ unary * etc...)
Object operator @ (const Object&); // again friend version instead of member version